package pl.mdziedzic.unittests.general;

import static org.mockito.Mockito.verify;

import java.util.Calendar;

import org.fest.assertions.Assertions;
import org.fest.assertions.GenericAssert;

import pl.mdziedzic.unittests.AUCTION_STATUS;
import pl.mdziedzic.unittests.Auction;
import pl.mdziedzic.unittests.CreateAuctionRequest;
import pl.mdziedzic.unittests.services.HISTORY_EVENT;
import pl.mdziedzic.unittests.services.HistoryProvider;
import pl.mdziedzic.unittests.services.RequestContext;

public class AuctionThen extends GenericAssert<AuctionThen, Auction> {

	protected AuctionThen(Auction auction) {
		super(AuctionThen.class, auction);
	}

	public static AuctionThen assertThat(Auction auction) {
		return new AuctionThen(auction);
	}

	public AuctionThen isCreated(CreateAuctionRequest request, RequestContext context) {

		isNotNull();

		Assertions.assertThat(actual.hasBids()).isFalse();
		Assertions.assertThat(actual.getName()).isEqualTo(request.getName());
		Assertions.assertThat(actual.getStartTime()).isSameAs(request.getStartTime());
		Assertions.assertThat(actual.getEndTime()).isSameAs(request.getEndTime());
		Assertions.assertThat(actual.getType()).isEqualTo(request.getType());
		Assertions.assertThat(actual.getStatus()).isEqualTo(AUCTION_STATUS.NEW);
		Assertions.assertThat(actual.getBids()).isEmpty();
		Assertions.assertThat(actual.getOwner()).isEqualTo(context.getUser());

		assertThatHistoryWasSaved(context, HISTORY_EVENT.AUCTION_CREATED);

		return this;
	}

	public AuctionThen isActive(RequestContext context) {

		isNotNull();

		Assertions.assertThat(actual.getActivateDate()).isNotNull();
		Assertions.assertThat(actual.getStatus()).isEqualTo(AUCTION_STATUS.ACTIVE);

		assertThatHistoryWasSaved(context, HISTORY_EVENT.AUCTION_ACTIVATED);

		return this;
	}

	// odkomentowac wraz z AuctionTest3
	private void assertThatHistoryWasSaved(RequestContext context, HISTORY_EVENT event) {
//		HistoryProvider historyProvider = context.getHistoryProvider();
//		verify(historyProvider).saveHistory(event);
	}

	// INFO Kod przeznaczony do prezentacji TDD

	public void startTimeChanged(RequestContext context, Calendar expectedStartTime) {
		Assertions.assertThat(actual.getStartTime()).isEqualTo(expectedStartTime);
		assertThatHistoryWasSaved(context, HISTORY_EVENT.AUCTION_DATE_CHANGED);
	}

	public void buyNowPriceIs(long expectedBuyNowPrice) {
		Assertions.assertThat(actual.getBuyNowPrice()).isEqualTo(expectedBuyNowPrice);
	}

	public void isCancelled(RequestContext context) {
		Assertions.assertThat(actual.getStatus()).isEqualTo(AUCTION_STATUS.CANCELLED);
		Assertions.assertThat(actual.getCancelDate()).isEqualTo(context.getNow());
	}

}
